/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.common.client.model.InformationModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *
 */
public class InformationComponent extends JSONServerComponent
{	
	protected boolean infoHorizontal;
	protected Grid bodyPanel;
	protected InformationModel informationModel;
	protected List<InformationModel> informationModels;

	public InformationComponent(StoreComponentController controller, String stylePrefix, boolean infoHorizontal)
	{
		super(controller, stylePrefix, null, false);
		this.infoHorizontal = infoHorizontal;
		informationModel = new InformationModel();
		informationModels = new ArrayList<InformationModel>();
	}
	
	public void onBuilt()
	{
		controller.getComponent("InformationDescriptionComponent").build();
		super.onBuilt();
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		informationModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		InformationModel model = new InformationModel(jsonObject);
		model.setIndex(informationModels.size());
		informationModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		int sz = informationModels.size();

		if (infoHorizontal)
			bodyPanel = new Grid(1, sz-1);
		else
			bodyPanel = new Grid(sz-1, 1);

		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		bodyPanel.addClickHandler(tableHandler); 
		CellFormatter cf = bodyPanel.getCellFormatter();
		for (int i = 0; i < sz; i++)
		{
			InformationModel model = informationModels.get(i);
			if (i > 0)
			{
				if (infoHorizontal) {
					bodyPanel.setText(0, i-1, model.getTitle());
					cf.addStyleName(0, i-1, getHandle() + "Item");
					cf.addStyleName(0, i-1, "ComponentLink");
				}
				else {
					bodyPanel.setText(i-1, 0, model.getTitle());
					cf.addStyleName(i-1, 0, getHandle() + "Item");
					cf.addStyleName(i-1, 0, "ComponentLink");

				}
			}
		}

		panel.setWidget(1, 0, bodyPanel);

		InformationModel model  = getInformationModel(0);
		informationModel.update(model);

		onBuilt();
	}

	protected ClickHandler tableHandler = new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int index = cell.getCellIndex()+1;
			InformationModel model  = getInformationModel(index);
			String description = model.getDescription();
			if (description.startsWith("http"))
			{
				Window.open(description, "_blank", "");
			}
			else if (description.startsWith("#"))
			{
				onSelection(description.substring(1));
			}
			else
			{
				onSelection("InformationDescriptionComponent--" + index);
			}
		}
	};

	public InformationModel getInformationModel(int index) {
		return informationModels.get(index);
	}

	public int getInformationModelsSize() {
		return informationModels.size();
	}

	public InformationModel getInformationModel() {
		return informationModel;
	}
}
